package com.xinxin.leetcode.problem682;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ShiXinXin
 * @date 2020-02-23 18:24
 */
class Solution {
    public int calPoints(String[] ops) {
        int sum = 0;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ops.length; i++) {
            if ("D".equals(ops[i])) {
                list.add(2 * list.get(list.size() - 1));
                sum += list.get(list.size() - 1);
            } else if ("+".equals(ops[i])) {
                list.add(list.get(list.size() - 1) + list.get(list.size() - 2));
                sum += list.get(list.size() - 1);
            } else if ("C".equals(ops[i])) {
                sum -= list.get(list.size() - 1);
                list.remove(list.size() - 1);
            } else {
                list.add(Integer.valueOf(ops[i]));
                sum += list.get(list.size() - 1);
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        String[] strs = {"5", "-2", "4", "C", "D", "9", "+", "+"};
        System.out.println(new Solution().calPoints(strs));
    }
}
